const {DBHOST, DBPORT, DBNAME} = require('../config/config')
// console.log(config);
module.exports = function (success, error) {
    // 如果没传error 则默认等于一个函数
    if (typeof error !== 'function') {
        error = () => {
            console.log('连接失败');
        }
    }
    const mongoose = require('mongoose')

    // 连接mongodb服务
    mongoose.connect(`mongodb://${DBHOST}:${DBPORT}/${DBNAME}`)


    mongoose.connection.on('open', () => {
        success()
    })


    mongoose.connection.on('error', () => {
        error()
    })

    mongoose.connection.on('close', () => {
        console.log('连接关闭');
    })


}